package com.ourcompany.apps.web;

import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Crawler {

    public static void main(String[] args)  {
        int k = 1;
    	try {
            Document doc = Jsoup.connect("http://www.autolanka.com/Buy.asp?ad_category_id=&search=&Page=2#members").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2").get();
            
            //Elements e = doc.select("font[class*=BuyDataFONT]");
            //Elements e = doc.select("a[href^=ad.asp?ID=");
            Elements e = doc.select("a[href]:containsOwn([More Details])");
            
            for (Iterator<Element> it = e.iterator(); it.hasNext();) {
                Element element = it.next();
                try {
                    Document innerDoc = Jsoup.connect("http://www.autolanka.com/" + element.outerHtml().substring(9, 25)).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2").get();
                    System.out.println("**************************************************");	
                    //Elements e = doc.select("font[class*=BuyDataFONT]");
                    //Elements e = doc.select("a[href^=ad.asp?ID=");
                    Elements innerEle = innerDoc.select("font[face=Arial, Helvetica, sans-serif]");
                    
                    for (Iterator<Element> InnerIt = innerEle.iterator(); InnerIt.hasNext();) {
                        Element InnerElement = InnerIt.next();
                        
                        System.out.println(InnerElement.text());
                        //k++;
                    }
                    System.out.println(k);	
                } catch (Exception ex) {
                    Logger.getLogger(Crawler.class.getName()).log(Level.SEVERE, null, ex);
                    System.out.println(ex.getMessage());
                }
                //System.out.println(element.outerHtml().substring(9, 25));
                k++;
                System.out.println("*************************" + k + "*************************");
            }
            	
        } catch (Exception ex) {
            Logger.getLogger(Crawler.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex.getMessage());
        }
        
    }
}
